<?php
namespace app\admin\controller;

use app\common\model\RoleModel;

/**
 * 管理员角色
 */
class Role extends Common
{
    /**
     * 添加角色
     * @author 贺强
     * @time   2021-09-14 14:43:50
     * @param  RoleModel $r RoleModel 实例
     */
    public function add(RoleModel $r)
    {
        if ($this->request->isAjax()) {
            $param = $this->request->post();
            if (empty($param['role_name'])) {
                return ['status' => 1, 'info' => '非法参数'];
            }
            $has = $r->getCount(['role_name' => $param['role_name']]);
            if (!empty($has)) {
                return ['status' => 2, 'info' => '该角色已存在'];
            }
            $res = $r->add($param);
            if (!$res) {
                return ['status' => 4, 'info' => '添加失败'];
            }
            return ['status' => 0, 'info' => '添加成功'];
        } else {
            return $this->fetch('add');
        }
    }

    /**
     * 设置管理员角色
     * @author 贺强
     * @time   2021-09-14 15:17:24
     * @param  RoleModel $r RoleModel 实例
     */
    public function operate(RoleModel $r)
    {
        $ids = $this->request->post('ids');
        if (empty($ids) || !preg_match('/^0[\,\d+]+$/', $ids)) {
            return ['status' => 3, 'info' => '非法参数'];
        }
        $type = $this->request->post('type');
        if (empty($type)) {
            return ['status' => 1, 'info' => '非法操作'];
        }
        if ($type === 'del' || $type === 'delAll') {
            $res = $r->delByWhere([['id', 'in', $ids]]);
            if ($res) {
                return ['status' => 0, 'info' => '删除成功'];
            } else {
                return ['status' => 4, 'info' => '删除失败'];
            }
        } elseif ($type === 'disable' || $type === 'disableAll') {
            $field = 'status';
            $value = 4;
            $msg   = '禁用';
        } elseif ($type === 'enable' || $type == 'enableAll') {
            $field = 'status';
            $value = 1;
            $msg   = '启用';
        } else {
            return ['status' => 2, 'info' => '非法操作'];
        }
        $res = $r->modifyField($field, $value, [['id', 'in', $ids]]);
        if ($res) {
            return ['status' => 0, 'info' => $msg . '成功'];
        } elseif ($res === false) {
            return ['status' => 4, 'info' => $msg . '失败'];
        } else {
            return ['status' => 5, 'info' => '该账号已' . $msg];
        }
    }

    /**
     * 管理员角色
     * @author 贺强
     * @time   2021-09-14 15:15:29
     * @param  RoleModel $r RoleModel 实例
     */
    public function lists(RoleModel $r)
    {
        $where = [['id', '<>', 1]];
        $param = $this->request->get();
        // 分页参数
        $page     = $param['page'] ?? 1;
        $pagesize = $param['pagesize'] ?? config('PAGESIZE');
        $list     = $r->getList($where, true, "$page,$pagesize");
        // var_dump($list);exit;
        foreach ($list as &$item) {
            $item['status_txt'] = get_role_status($item['status']);
            if (!empty($item['ctime'])) {
                format_datetime($item['ctime']);
            }
        }
        $count = $r->getCount($where);
        $pages = ceil($count / $pagesize);
        return $this->fetch('list', compact('list', 'pages'));
    }
}
